package com.jinzhi.jzweb.controller;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.jinzhi.jzweb.domain.BookOrderDO;
import com.jinzhi.jzweb.domain.IntegralUserDetailDO;
import com.jinzhi.jzweb.domain.SysBrandVO;
import com.jinzhi.jzweb.pojo.RefundVO;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.SysBrandDO;
import com.jinzhi.jzweb.service.SysBrandService;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2022-09-21 10:45:57 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/sysBrand")
public class SysBrandController extends BaseController {
	@Autowired
	private SysBrandService sysBrandService;

	@Autowired
	private UserService userService;

	@GetMapping()
	@RequiresPermissions("jzweb:sysBrand:sysBrand")
	String SysBrand(){
	    return "jzweb/sysBrand/sysBrand";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:sysBrand:sysBrand")
	public Result<Page<SysBrandDO>> list(SysBrandDO sysBrandDTO){
		// 查询列表数据
		EntityWrapper<SysBrandDO> wrapper = new EntityWrapper<>();
		wrapper.orderBy("create_time",false);
		if(StringUtils.isNotBlank(sysBrandDTO.getBrandName())){
			wrapper.eq("brand_name",sysBrandDTO.getBrandName());
		}

		Page<SysBrandDO> page = sysBrandService.selectPage(getPage(SysBrandDO.class),wrapper
					);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				SysBrandDO obj = (SysBrandDO) page.getRecords().get(i);
				if (obj.getUserId()!=null && !obj.getUserId().equals("")){
					UserDO userDO = userService.selectById(obj.getUserId());
					obj.setUserName(userDO.getName());
				}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}





	/**
	 * 全部导出 Excel版
	 *
	 */
	@GetMapping("/brandImport")
	@ResponseBody
	@Transactional
	public void brandImport(HttpServletResponse response){
		// 查询列表数据
		EntityWrapper<SysBrandDO> wrapper = new EntityWrapper<>();
		wrapper.orderBy("create_time",false);
		List<SysBrandDO> sysBrandDOS = sysBrandService.selectList(wrapper);

		List<SysBrandDO> list = new ArrayList();
		for (int i = 0; i < sysBrandDOS.size(); i++) {
			SysBrandDO obj =  sysBrandDOS.get(i);
			if (obj.getUserId()!=null && !obj.getUserId().equals("")){
				UserDO userDO = userService.selectById(obj.getUserId());
				obj.setUserName(userDO.getName());
			}
			list.add(obj);
		}

		List<SysBrandVO> listFile =new ArrayList<>();
		for (int i=0;i<list.size();i++){
			SysBrandVO sysBrandVO =new SysBrandVO();
			sysBrandVO.setBrandName(list.get(i).getBrandName()); //名称
			if (list.get(i).getBrandType() == 0){ //类型
				sysBrandVO.setBrandType("幼儿照护");
			}else {
				sysBrandVO.setBrandType("产后恢复");
			}
			if (list.get(i).getEnterpriseScale().equals("1")){
				sysBrandVO.setEnterpriseScale("0-20人"); //企业规模
			}
			if (list.get(i).getEnterpriseScale().equals("2")){
				sysBrandVO.setEnterpriseScale("20-50人"); //企业规模
			}
			if (list.get(i).getEnterpriseScale().equals("3")){
				sysBrandVO.setEnterpriseScale("50-100人"); //企业规模
			}
			if (list.get(i).getEnterpriseScale().equals("4")){
				sysBrandVO.setEnterpriseScale("100人以上"); //企业规模
			}
			sysBrandVO.setEnterpriseName(list.get(i).getEnterpriseName()); //企业名称
			sysBrandVO.setBrandlogo(list.get(i).getBrandlogo()); //logo
			sysBrandVO.setPersonInCharge(list.get(i).getPersonInCharge()); //负责人
			sysBrandVO.setTelephone(list.get(i).getTelephone()); //电话号码
			listFile.add(sysBrandVO);
		}
		EasyPoiUtil.exportExcel(listFile, "品牌机构信息表", "品牌机构信息表", SysBrandVO.class, "品牌机构信息表", response);
	}








	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:sysBrand:add")
	String add(){
	    return "jzweb/sysBrand/add";
	}

	@GetMapping("/brandDetails/{id}")
	@RequiresPermissions("jzweb:sysBrand:add")
	String brandDetails(@PathVariable("id") Long id,Model model){
		SysBrandDO sysBrand = sysBrandService.selectById(id);
		if(sysBrand.getUserId()!=null){
		UserDO userDO= userService.getByUserId(sysBrand.getUserId());
		if(userDO!=null){
			sysBrand.setUserIdName(userDO.getUsername());
		}

		}
		if(sysBrand.getToExamine()!=null){
			sysBrand.setToExamineName(userService.getByUserId(sysBrand.getToExamine()).getUsername());
		}
		if (sysBrand.getStatus()==0){
			sysBrand.setStatusNmae("未审核");
		}else if(sysBrand.getStatus()==1){
			sysBrand.setStatusNmae("审核通过");
		}else
		{
			sysBrand.setStatusNmae("审核未通过");
		}
		if (sysBrand.getState()==0){
			sysBrand.setStateNmae("正常");
		}else {
			sysBrand.setStateNmae("删除/作废");
		}
		if(sysBrand.getCreateTime()!=null){
			sysBrand.setCreateTimes(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(sysBrand.getCreateTime()));
		}
		if(sysBrand.getToExamineTime()!=null){
			sysBrand.setToExamineTimes(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(sysBrand.getToExamineTime()));
		}
		if(sysBrand.getUpdateTime()!=null){
			sysBrand.setUpdateTimes(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(sysBrand.getUpdateTime()));
		}




		model.addAttribute("sysBrand", sysBrand);
		return "jzweb/sysBrand/details";
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:sysBrand:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SysBrandDO sysBrand = sysBrandService.selectById(id);
		model.addAttribute("sysBrand", sysBrand);
		Map map1=new HashMap();

		List <Map> mapList=new ArrayList<>();
		map1.put("name","幼儿照护");
		mapList.add(map1);
		Map map2=new HashMap();
		map2.put("name","产后恢复");
		mapList.add(map2);
		model.addAttribute("list2",mapList);
	    return "jzweb/sysBrand/edit";
	}

	@PostMapping("/review")
	@ResponseBody
	@RequiresPermissions("jzweb:sysBrand:edit")
	Result<String> review(@RequestParam("ids[]") Long[] ids,Integer state,String remark){
		List<SysBrandDO> sysBrandDOS = sysBrandService.selectBatchIds(Arrays.asList(ids));
		for (int i=0;i<sysBrandDOS.size();i++){
			if (state == 1){
				sysBrandDOS.get(i).setStatus(1);
				sysBrandDOS.get(i).setRemarks("后台审核通过！");
			}else if (state == 2){
				sysBrandDOS.get(i).setStatus(2);
				sysBrandDOS.get(i).setRemarks(remark);
			}
		}

		sysBrandService.updateBatchById(sysBrandDOS);
		return Result.build(0,"审核成功！");
	}



//	@PostMapping("/review")
//	@ResponseBody
//	@RequiresPermissions("jzweb:sysBrand:edit")
//	Result<String> review(@RequestParam("ids[]") Long[] ids,Integer state,String remark){
//		List<SysBrandDO> sysBrandDOS = sysBrandService.selectBatchIds(Arrays.asList(ids));
//
//		List<UserDO> userDOS =new ArrayList<>();  //用户表
//		List<IntegralUserDetailDO> list =new ArrayList<>();  //积分修改记录表
//
//		for (int i=0;i<sysBrandDOS.size();i++){
//			if (state == 1){
//				sysBrandDOS.get(i).setStatus(1);
//				sysBrandDOS.get(i).setRemarks("后台审核通过！");
//				if (sysBrandDOS.get(i).getUserId()!=null&&!sysBrandDOS.get(i).equals("")) {
//					UserDO user = userService.selectById(sysBrandDOS.get(i).getUserId());
//					if (user.getIntegral() == null || "".equals(user.getIntegral())) {
//						user.setIntegral("0");
//					}
//					Integer sum = Integer.valueOf(user.getIntegral()) + 3000;
//					user.setIntegral(sum.toString());
//					IntegralUserDetailDO integralUserDetail = new IntegralUserDetailDO();
//					integralUserDetail.setIntegralDetail("+3000"); //扣除积分
//					integralUserDetail.setUserId(Long.valueOf(user.getId())); //用户id
//					integralUserDetail.setIntegralGainDate(new Date()); //积分获取时间
//					integralUserDetail.setIntegralGainPathway("企业入驻");
//					list.add(integralUserDetail);
//					userDOS.add(user);
//				}
//			}else if (state == 2){
//				sysBrandDOS.get(i).setStatus(2);
//				sysBrandDOS.get(i).setRemarks(remark);
//			}
//		}
//		userService.updateBatchById(userDOS);
//		sysBrandService.updateBatchById(sysBrandDOS);
//		return Result.build(0,"审核成功！");
//	}


	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:sysBrand:add")
	public Result<String> save( SysBrandDO sysBrand){
		sysBrand.setCreateTime(LocalDateTime.now());
		sysBrandService.insert(sysBrand);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:sysBrand:edit")
	public Result<String>  update( SysBrandDO sysBrand){
		boolean update = sysBrandService.updateById(sysBrand);
		return update ? Result.ok() : Result.fail();
	}

	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:sysBrand:remove")
	public Result<String>  remove( Long id){
		sysBrandService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:sysBrand:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		sysBrandService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
